<?php
/**
 * @name IMAGE: Image process library extension
 * @version 0.1, February 06, 2008
 * @copyright &copy; 2008 Novachok, A.
 * @author Snow (Novachok Alexandr) <novachok@gmail.com>
 * @package Image Process Library
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * >
 */

class Image
{
   var $source = array();
   var $destination;
   var $params = array();
   var $error  = false;
   
   /**
    * Set parameters
    *
    * @param string $params
    */
   function SetParams($params = false)
   {
      $this->params['prop']  = false;
      $this->params['less']  = false;
      $this->params['small'] = false;
      $this->params['dw']    = 0;
      $this->params['dh']    = 0;
      $this->params['dr']    = 0;
       
      if($params != false)
      {
         $params = explode(';', $params);

         foreach($params AS $param)
         {
            $param = explode('=', $param);
            switch($param[0])
            {
               case 'proportion':
               case 'p':
                  $this->params['prop'] = isBool($param[1]);
                  break;

               case 'by_less':
               case 'l':
                  $this->params['less'] = isBool($param[1]);
                  break;

               case 'small':
               case 's':
                  $this->params['small'] = isBool($param[1]);
                  break;

               case 'width':
               case 'w':
                  $this->params['dw'] = isDigit($param[1]);
                  break;

               case 'height':
               case 'h':
                  $this->params['dh'] = isDigit($param[1]);
                  break;
            }
         }

         if(isset($this->params['dw']) && isset($this->params['dh']))
         {
            $this->params['dr'] = $this->params['dw'] / $this->params['dh'];
         } else
         {
            $this->error = true;
         }
      }
   }


   /**
    * Read params from source image
    *
    * @param string $image - file path to source image
    */
   function SetSource($image)
   {
      $image_params = getimagesize($image);
      if($image_params == false)
      {
         $this->source = false;
      } else
      {
         $this->source['width'] = $image_params[0];
         $this->source['height'] = $image_params[1];
         $this->source['ratio'] = $image_params[0] / $image_params[1];
         $this->source['mime'] = $image_params['mime'];

         switch ($image_params['mime'])
         {
            case 'image/jpeg':
            $this->source['image'] = imagecreatefromjpeg($image);
            break;

            case 'image/png':
            $this->source['image'] = imagecreatefrompng($image);
            break;

            case 'image/gif':
            $this->source['image'] = imagecreatefromgif($image);
            break;
         }
      }
   }
   
   /**
    * Resize Image
    *
    * @param string $image
    * @param string $destination
    * @return array|bool
    */
   function Resize($image, $destination)
   {
      if($this->error == true) return false;
      $result = false;

      $this->SetSource($image);
      if($this->params['dw'] == $this->source['width'] && $this->params['dh'] == $this->source['height'])
      {
         $result = $this->CopyFile($image, $destination);
         var_dump($result);
      } else
      {
         $k = $this->CalculateK();
         if($k < 1 || $this->params['small'] == true)
         {
            $w = $ww= round($this->source['width']*$k);
            $h = $hh = round($this->source['height']*$k);
            $sx = 0;
            $sy = 0;

            if($this->params['prop'] == true)
            {
               if($this->source['ratio'] > $this->params['dr'])
               {
                  $sx = ($w - $this->params['dw']) / 2;
                  $w  = $this->params['dw'];
               } else
               {
                  $sy = ($h - $this->params['dh']) / 2;
                  $h  = $this->params['dh']; 
               }
            }

            $this->destination = imagecreatetruecolor($w, $h);
            imagecopyresampled(
               $this->destination, 
               $this->source['image'], 
               0,
               0, 
               $sx, 
               $sy,
               $ww, 
               $hh, 
               $this->source['width'], 
               $this->source['height']
            );

            if($this->WriteImage($destination))
            {
               $result['width'] = $w;
               $result['height'] = $h;
               $result['mime'] = $this->source['mime'];
            }
         } else
         {
            $result = $this->CopyFile($image, $destination);
         }
      }

      return $result;
   }

   /**
    * Calculate Koefficient for image resize
    *
    * @return float
    */
   function CalculateK()
   {
      $k = 1;
      if($this->source['ratio'] > $this->params['dr'] || $this->source['ratio'] == $this->params['dr'])
      {
         if($this->params['prop'] == true || $this->params['less'] == true)
         {
            $k = $this->params['dh'] / $this->source['height'];
         } else
         {
            $k = $this->params['dw'] / $this->source['width'];
         }
      } elseif($this->source['ratio'] < $this->params['dr'])
      {
         if($this->params['prop'] == true || $this->params['less'] == true)
         {
            $k = $this->params['dw'] / $this->source['width'];
         } else
         {
            $k = $this->params['dh'] / $this->source['height'];
         }
      }

      return $k;
   }

   /**
    * Write image
    *
    * @param strint $destination
    * @return bool
    */
   function WriteImage($destination)
   {
      switch ($this->source['mime'])
      {
         case 'image/jpeg':
         $result = @imagejpeg($this->destination, $destination);
         break;

         case 'image/png':
         $result = @imagepng($this->destination, $destination);
         break;

         case 'image/gif':
         $result = @imagegif($this->destination, $destination);
         break;
      }

      return $result;
   }

   function CopyFile($source, $destination)
   {
      $result['width'] = $this->source['width'];
      $result['height'] = $this->source['height'];
      $result['mime'] = $this->source['mime'];

      if($source != $destination)
      {
         if(!copy($source, $destination))
         {
            $result = false;
         }
      }
      return $result;
   }
}
?>